<?php
/**
* 名称：内参资讯模块控制器。
* 描述：内参资讯模块相关的控制响应程式。
*/
final class Controller_IRRS extends Controller{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Main(){
        $data=array(
            'base'=>array(
                'IRRS0002'=>Model_IRRS::GetDataX0002(),
                'IRRS0003'=>Model_IRRS::GetDataX0003(),
                'IRRS0004'=>array(
                    'Name'=>'内参资讯',
                    'Link'=>Route::MakeHyperlink(':irrs::listview'),
                    'Rows'=>Model_IRRS::MakeLinkForDetail(self::$Database->GetAll(
                        'SELECT * FROM '.self::$Database->Table('irrs').' ORDER BY datetime DESC LIMIT 0,5'
                    ))
                ),
                'ad'=>Model_Ad::IRRS()
            )
        );
        //-
        $rows=Model_Exhibition::MakeLinkForDetail(self::$Database->GetAll(
            'SELECT * FROM '.self::$Database->Table('exhibition').' ORDER BY datetime DESC LIMIT 0,20'
        ));
        if(count($rows)>1){
           $rows['top']=$rows[0];
           $rows['common']=array_slice($rows,1,count($rows));
        }
        else{
           $rows['top']=$rows;
        }
        $data['base']['exhibition0001']=array(
            'Name'=>'行业展会','Link'=>Route::MakeHyperlink(':exhibition::listview'),'Rows'=>$rows
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('irrs/index.html');
    }

    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $arguments=array(
            'type'=>Route::GetArguments('type'),'class'=>(int)Route::GetArguments('class')
        );
        //~
        $scalar=array(
            'table'=>self::$Database->Table('irrs'),
            'sql'=>$this->Function_Listview_SQL($arguments['type'],$arguments['class'])
        );
        //~
        $data=array(
            'meta'=>array(
                'breadcrumb'=>$this->Function_Listview_Breadcrumb($arguments['type'],$arguments['class'])
            ),
            'base'=>array(
                'list'=>self::$Database->GetLimit($scalar['sql'],Route::GetArguments('page'),20),
                'IRRS0003'=>Model_IRRS::GetDataX0003(),
                'IRRS0002'=>Model_IRRS::GetDataX0002(),
                'ad'=>Model_Ad::IRRS()
            ),
            'arguments'=>array_filter($arguments)
        );
        $data['base']['list']['Rows']=Model_IRRS::MakeLinkForDetail($data['base']['list']['Rows']);
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('irrs/list.html');
    }

    /**
    * 描述：详细信息程式。
    * @access protected
    * @return void
    */
    protected function Detail(){
        $arguments=array(
            'id'=>(int)Route::GetArguments('id'),
        );
        //~
        $scalar=array(
            'class'=>self::$Database->GetOne('SELECT class FROM '.self::$Database->Table('irrs').'WHERE id=?',array($arguments['id']))
        );
        //~
        $data=array(
            'meta'=>array(
                'breadcrumb'=>array(
                    'listName'=>self::$Database->GetOne('SELECT name FROM '.self::$Database->Table('irrs_class').'WHERE id=?',array($scalar['class'])),
                    'listLink'=>Route::MakeHyperlink(':irrs::listview',array('class'=>$scalar['class']))
                )
            ),
            'base'=>array(
                'main'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('irrs').' WHERE id=?',
                    array($arguments['id'])
                ),
                'previous'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('irrs').' WHERE id>? AND class=? ORDER BY datetime ASC',
                    array($arguments['id'],$scalar['class'])
                ),
                'next'=>self::$Database->GetRow(
                    'SELECT * FROM '.self::$Database->Table('irrs').'  WHERE id<? AND class=? ORDER BY datetime DESC',
                    array($arguments['id'],$scalar['class'])
                ),
                'IRRS0003'=>Model_IRRS::GetDataX0003(),
                'IRRS0002'=>Model_IRRS::GetDataX0002(),
                'ad'=>Model_Ad::IRRS()
            )
        );
        //~
        if(empty($data['base']['main'])){
            Route::Http404();
            return;
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('irrs/detail.html');
    }

    private function Function_Listview_Breadcrumb($type,$class){
        $var=array(
            'listName'=>'最新资讯',
            'listLink'=>Route::MakeHyperlink(':irrs::listview'),
            'listNameForType'=>array(
            )
        );
        //~
        if(empty($type)){
            if($class>0){
                $var['listName']=self::$Database->GetOne('SELECT name FROM '.self::$Database->Table('irrs_class').' WHERE id='.$class);
                $var['listLink']=Route::MakeHyperlink(':irrs::listview',array('class'=>$class));
            }
        }
        else{
            if(isset($var['listNameForType'][$type])){
                $var['listName']=$var['listNameForType'][$type];
                $var['listLink']=Route::MakeHyperlink(':irrs::listview',array('type'=>$type));
            }
        }
        //~
        return array('listName'=>$var['listName'],'listLink'=>$var['listLink']);
    }

    private function Function_Listview_SQL($type,$class){
        switch($type){
            default:
                if($class>0){
                    return 'SELECT * FROM '.self::$Database->Table('irrs').' WHERE class='.$class.' ORDER BY datetime DESC';
                }
                else{
                    return 'SELECT * FROM '.self::$Database->Table('irrs').' ORDER BY datetime DESC';
                }
        }
    }
}